#!/usr/bin/env python 3.6
# -*- coding: utf-8 -*-
# @Time      :2021/6/21
# @Author    :zhangxuchuo
# @Email     :zhangxuchuo@bbdservice.com
# @File      :TAX45_Ent12MonsIncomeTaxAmtCheck_SC
# @Software  :PyCharm


'''eof
name:申请企业近12月所得税应纳税额（合计）
code:TAX45_Ent12MonsIncomeTaxAmtCheck_SC
tableName:
columnName:
groups:场景业务校验-税务
dependencies:TX_SC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import json
import re
import sys
import time,datetime
import pandas as pd
import math
from dateutil import rrule
from dateutil.relativedelta import relativedelta
import io

# reload(sys)
# sys.setdefaultencoding('utf-8')


def inquireIsReportNo(data):
    """
    判断是否存在reportNo
    :param data:
    :return:True/False
    """
    try:

        is_reportNo = True if len(data.get("reportNo")) > 0 else False

    except:
        is_reportNo = False

    return is_reportNo

def formatData(table_Name):
    """
    获取数据
    :param table_Name:字典keys
    :return:[{}]
    """
    try:
        data = TX_SC_DSJ["data"].get(table_Name)
        return data if isinstance(data, list) and len(data) > 0 else [{}]

    except:
        return [{}]

def dataPre(dataList=None, key=None):
    """
    对list里面的列表去重月份不标准的
    :param dataList:
    :param key:
    :return:
    """
    dataListTemp = dataList
    data = []
    for i in range(len(dataListTemp)):
        temp = dataListTemp[i].get(key)
        try:
            temp = int(temp)
        except:
            temp = None
            pass

        if temp:
            data.append(dataListTemp[i])
        else:
            pass

    if len(data) == 0:
        data = [{}]
    else:
        pass

    return data

def convertDataType(data_value, data_type):
    """
    数据格式转换
    :param data_value:
    :param data_type: float/int/str/date_time
    :return:
    """
    data_value = str(data_value)
    return_data = None
    # float
    if data_type == 'float':
        try:
            return_data = float(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # int
    elif data_type == 'int':
        try:
            return_data = int(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # str
    elif data_type == 'str':
        try:
            return_data = str(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # date_time
    elif data_type == 'date_time':
        r = re.compile(r'\D+')
        try:
            return_data = datetime.datetime.strptime(data_value, '%Y.%m.%d').strftime('%Y-%m-%d')
        except:
            try:
                return_data = datetime.datetime.strptime(data_value, '%Y-%m-%d').strftime('%Y-%m-%d')
            except:
                try:
                    return_data = datetime.datetime.strptime(data_value, '%Y/%m/%d').strftime('%Y-%m-%d')
                except:
                    try:
                        data_value = r.sub('', data_value)
                        return_data = datetime.datetime.strptime(data_value, '%Y%m%d').strftime('%Y-%m-%d')
                    except:
                        return_data = None

    return return_data



def count_qz(data,time1,time2,time3,index,list):
    if data != [{}]:
        data = sorted(data, key=lambda x: int(x[time3]))
        for i in range(len(data)):

            try:
                ND_q = int(data[i][time1][0:4])
                ND_z = int(data[i][time2][0:4])
                YF_q = int(data[i][time1][5:7])
                YF_z = int(data[i][time2][5:7])

                if ND_q == ND_z and YF_q == YF_z:
                    if ND_z and YF_z:
                        NDYF = str(ND_z) + str(YF_z)
                        if NDYF in list.index:
                            tax_amount = convertDataType(data[i].get(index), 'float')
                            if tax_amount is not None:
                                if math.isnan(list.loc[str(ND_z) + str(YF_z)]):
                                    list.loc[str(ND_z) + str(YF_z)] = tax_amount
                                else:
                                    pass
                elif ND_q != ND_z or YF_q != YF_z:
                    time_q = datetime.datetime(ND_q, YF_q, 1)
                    time_z = datetime.datetime(ND_z, YF_z, 1)
                    time_gap = rrule.rrule(rrule.MONTHLY, dtstart=time_q, until=time_z).count()
                    for j in range(time_gap):
                        # print(j)
                        change_time = time_z - relativedelta(months=+j)
                        # print(change_time)
                        ND = int(change_time.year)
                        YF = int(change_time.month)
                        if ND and YF:
                            NDYF = str(ND) + str(YF)
                            if NDYF in list.index:
                                tax_amount = convertDataType(data[i].get(index), 'float')
                                if tax_amount is not None:
                                    if math.isnan(list.loc[str(ND) + str(YF)]):
                                        list.loc[str(ND) + str(YF)] = tax_amount / time_gap
                                    else:

                                        pass
                else:
                    pass
            except:


                pass
    else:
        pass
    return list

def vatDataMerge(syptZzsxgm, syptZzsybnsr, syptSwdjxx,putBackYear=2, CcrDate=None):

    """
    增值税纳税数据拆分为近两年的月度数据

    :param syptZzsxgm:小规模增值税纳税表[{}]；
    :param syptZzsybnsr:一般纳税人增值税表[{}]；
    :param putBackYear:回推时间，默认为2年；
    :param CcrDate:测试时间，默认为None，格式为datetime.date(2019, 2, 19)；
    :return: VatData为Series
    """

    # 字典排序
    syptZzsxgm = dataPre(syptZzsxgm,key='YF')
    syptZzsybnsr = dataPre(syptZzsybnsr, key='YF')
    NSRZG_DM=syptSwdjxx[0].get('NSRZG_DM')
    # 生成时间轴
    dateIndexList = []
    if CcrDate is None:
        CcrDate = datetime.datetime.now().strftime('%Y-%m-%d')
        StartDate = str(datetime.datetime.now().year - putBackYear) + CcrDate[4:]
        dateSet = pd.date_range(StartDate, CcrDate, freq='M')
    else:
        CcrDate = datetime.datetime.strptime(convertDataType(CcrDate, 'date_time'), '%Y-%m-%d').date()
        StartDate = str(CcrDate.year - putBackYear) + convertDataType(CcrDate, 'date_time')[4:]
        dateSet = pd.date_range(StartDate, CcrDate, freq='M')

    for i in dateSet:
        year = i.date().year
        month = i.date().month
        dateIndexList.append(str(year) + str(month))

    VatData = pd.Series(None, index=dateIndexList)


    if syptZzsxgm == [{}] and syptZzsybnsr == [{}]:
        return VatData
    else:
        # 一般纳税人
        if NSRZG_DM==u'204'  or  NSRZG_DM==u'205':
            NSRZG='xgn'
        else:
            NSRZG = 'yb'
        if NSRZG == 'yb':
            # print(VatData)
            VatData=count_qz(data=syptZzsybnsr, time1='skssqq', time2='skssqz', time3='YF', index='YNSEHJ', list=VatData)
            VatData = count_qz(data=syptZzsxgm,time1='skssqq',time2='skssqz',time3='YF',index='YNSEHJDBNBQ',list=VatData)
        else:
            VatData = count_qz(data=syptZzsxgm, time1='skssqq', time2='skssqz', time3='YF', index='YNSEHJDBNBQ',
                               list=VatData)

            VatData = count_qz(data=syptZzsybnsr, time1='skssqq', time2='skssqz', time3='YF', index='YNSEHJ',
                               list=VatData)


    return VatData

def incomeTaxDataMerge(syptQysdsNd, syptQysdsA1, putBackYear=2, CcrDate=None):
    """
    所得税纳税数据拆分为近2年的月度数据

    :param syptQysdsNd: 年度汇算清缴表，类型[{}]
    :param syptQysdsA1: A1表，类型[{}]
    :param putBackYear: 回推时间，默认为2年
    :param CcrDate: 测试时间，默认为None，格式为datetime.date(2019, 2, 19)；
    :return: IncomeTaxData为Series
    """
    # 字典排序
    syptQysdsNd = dataPre(syptQysdsNd, key='ND')
    syptQysdsA1 = dataPre(syptQysdsA1, key='YF')

    # 生成时间轴
    dateIndexList = []
    # 用于生产
    if CcrDate is None:
        CcrYearMonth = datetime.datetime.now().strftime('%Y-%m-%d')[:7]
        CcrDate = CcrYearMonth + '-01'
        StartDateTmp = str(datetime.datetime.now().year - putBackYear) + CcrDate[4:]
        StartDate = StartDateTmp[:7] + '-01'
    else:
        CcrYear = datetime.datetime.strptime(convertDataType(CcrDate, 'date_time'), '%Y-%m-%d').date().year
        CcrDate = convertDataType(CcrDate, 'date_time')[:7] + '-01'
        StartDate = str(CcrYear - putBackYear) + convertDataType(CcrDate, 'date_time')[4:]

    dateSet = pd.date_range(StartDate, CcrDate, freq='M')

    for i in dateSet:
        year = i.date().year
        month = i.date().month
        dateIndexList.append(str(year) + str(month))

    IncomeTaxData = pd.Series(None, index=dateIndexList)

    if syptQysdsNd == [{}] and syptQysdsA1 == [{}]:
        return IncomeTaxData
    else:
        # 使用年度汇算清缴表
        IncomeTaxData = count_qz(data=syptQysdsNd, time1='skssqq', time2='skssqz', time3='ND', index='YNSDSE', list=IncomeTaxData)
        # 使用A1表(默认为只有按季度缴
        IncomeTaxData = count_qz(data=syptQysdsA1, time1='skssqq', time2='skssqz', time3='YF', index='YNSSDE',
                                 list=IncomeTaxData)


        return IncomeTaxData


def countdownTwoYearsTaxAmtCheck(spDate=None):
    try:
        """
        企业近两年纳税额校验（同时有A1表和年度汇算清缴表）
        :return:
        """
        syptQysdsNd = formatData('syptQysdsNd')
        syptQysdsA1 = formatData('syptQysdsA1')
        syptZzsxgm = formatData('syptZzsxgm')
        syptZzsybnsr = formatData('syptZzsybnsr')
        syptSwdjxx = formatData('syptSwdjxx')

        # 增值税数据合并
        if syptZzsxgm == [{}] and syptZzsybnsr == [{}] and syptQysdsA1 == [{}] and syptQysdsNd == [{}]:
            return u'缺失值'
        else:
            vatDataSeries = vatDataMerge(syptZzsxgm=syptZzsxgm, syptZzsybnsr=syptZzsybnsr, syptSwdjxx=syptSwdjxx,CcrDate=spDate)
            vatDataSeries = vatDataSeries.fillna(value=0)

        # 所得税数据合并
        if syptZzsxgm == [{}] and syptZzsybnsr == [{}] and syptQysdsA1 == [{}] and syptQysdsNd == [{}]:
            return u'缺失值'
        else:
            incomeDataSeries = incomeTaxDataMerge(syptQysdsNd=syptQysdsNd, syptQysdsA1=syptQysdsA1, CcrDate=spDate)
            incomeDataSeries = incomeDataSeries.fillna(value=0)

        res1 = incomeDataSeries
        res2 = sum(incomeDataSeries)
        res3 = sum(incomeDataSeries[12:])
        #res4 = res3 / sum(incomeDataSeries[:12]) - 1

        return res3
    except:
        return u'缺失值'

result = countdownTwoYearsTaxAmtCheck()
# if __name__ == '__main__':
#     import json
#     with open(r'C:\Users\john\Desktop\samples\TX_SC_DSJ.json',encoding='utf8') as ff:
#         TX_SC_DSJ = json.loads(ff.read())['parsedContent']
#     cQexecuteTime = ""
#     result = countdownTwoYearsTaxAmtCheck()
#     print(result)
